<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\Border;

class DailyAmountExport implements FromCollection, WithStrictNullComparison, WithEvents
{
    private $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        $title = ["记录ID", "店铺ID", "公司总余额", "会员总余额", "悦享惠总金额", "报单奖励总金额", "分虹总金额", "招商奖励总金额", "教育培训基金总金额", "生成时间"];
        $data = $this->data;

        return collect([
            $title,
            $data
        ]);
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function (AfterSheet $event) {
                $begin = 2;
                $end = 2;
                $data = $this->data;
                $c = count($data) < 500 ? 100 : count($data);
                foreach($data as $key => $item)
                {
                    if($key > 0) {
                        if($data[$key-1]['id'] != $data[$key]['id']){
                            $end = $key + 1;
                            if($begin != $end) {
                                $event->sheet->getDelegate()->mergeCells("A$begin:A$end");
                                $event->sheet->getDelegate()->mergeCells("B$begin:B$end");
                                $event->sheet->getDelegate()->mergeCells("C$begin:C$end");
                                $event->sheet->getDelegate()->mergeCells("D$begin:D$end");
                                $event->sheet->getDelegate()->mergeCells("E$begin:E$end");
                                $event->sheet->getDelegate()->mergeCells("F$begin:F$end");
                                $event->sheet->getDelegate()->mergeCells("G$begin:G$end");
                                $event->sheet->getDelegate()->mergeCells("H$begin:H$end");
                                $event->sheet->getDelegate()->mergeCells("I$begin:I$end");
                                $event->sheet->getDelegate()->mergeCells("J$begin:J$end");
                            }
                            $begin = $key + 2;
                        }

                        if(!isset($data[$key+1])){
                            $end = $key + 2 ;
                            $event->sheet->getDelegate()->mergeCells("A$begin:A$end");
                            $event->sheet->getDelegate()->mergeCells("B$begin:B$end");
                            $event->sheet->getDelegate()->mergeCells("C$begin:C$end");
                            $event->sheet->getDelegate()->mergeCells("D$begin:D$end");
                            $event->sheet->getDelegate()->mergeCells("E$begin:E$end");
                            $event->sheet->getDelegate()->mergeCells("F$begin:F$end");
                            $event->sheet->getDelegate()->mergeCells("G$begin:G$end");
                            $event->sheet->getDelegate()->mergeCells("H$begin:H$end");
                            $event->sheet->getDelegate()->mergeCells("I$begin:I$end");
                            $event->sheet->getDelegate()->mergeCells("J$begin:J$end");
                        }
                    }
                }

                $event->sheet->getDelegate()->getColumnDimension('A')->setWidth(10);
                $event->sheet->getDelegate()->getColumnDimension('B')->setWidth(12);
                $event->sheet->getDelegate()->getColumnDimension('C')->setWidth(20);
                $event->sheet->getDelegate()->getColumnDimension('D')->setWidth(12);
                $event->sheet->getDelegate()->getColumnDimension('E')->setWidth(30);
                $event->sheet->getDelegate()->getColumnDimension('F')->setWidth(30);
                $event->sheet->getDelegate()->getColumnDimension('G')->setWidth(30);
                $event->sheet->getDelegate()->getColumnDimension('H')->setWidth(30);
                $event->sheet->getDelegate()->getColumnDimension('I')->setWidth(50);
                $event->sheet->getDelegate()->getColumnDimension('J')->setWidth(25);
                $event->sheet->getDelegate()->getStyle("A1:J1")->getFont()->setBold(true);
                $event->sheet->getDelegate()->getStyle("A1:J$c")->getAlignment()->setVertical('center');
                $event->sheet->getDelegate()->getStyle("A1:J$c")->getAlignment()->setHorizontal('center');
                $event->sheet->getDelegate()->getStyle("A1:J$c")->applyFromArray([
                    'borders' => [
                        'allBorders' => [
                            'borderStyle' =>Border::BORDER_THIN,
                            'color' => ['argb' => '999999']
                        ]
                    ]
                ]);
            }
        ];
    }
}
